home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bisonpcb.zip / LR0.C < prev    next >
C/C++ Source or Header  |  1987-02-13  |  14KB  |  652 lines

  1. /* Generate the nondeterministic finite state machine for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.   
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.   
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.   
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /* See comments in state.h for the data structures that represent it.
  22.    The entry point is generate_states.  */
  23.  
  24. /*
  25.  * Port to PC by Whit Gregg
  26.  *               Nourse, Gregg & Browne, Inc.
  27.  *         1 Horizon Road
  28.  *         Fort Lee, NJ  07024
  29.  */
  30.  
  31.  
  32. #include <stdio.h>
  33. #include <malloc.h>
  34. #include "machine.h"
  35. #include "new.h"
  36. #include "gram.h"
  37. #include "state.h"
  38. #include "func.h"
  39.  
  40.  
  41.     extern char *nullable;
  42. extern short *itemset;
  43. extern short *itemsetend;
  44.  
  45.  
  46. int nstates;
  47. int final_state;
  48. core *first_state;
  49. shifts *first_shift;
  50. reductions *first_reduction;
  51.  
  52. int get_state();
  53. core *new_state();
  54.  
  55. static core *this_state;
  56. static core *last_state;
  57. static shifts *last_shift;
  58. static reductions *last_reduction;
  59.  
  60. static int nshifts;
  61. static short *shift_symbol;
  62.  
  63. static short *redset;
  64. static short *shiftset;
  65.  
  66. static short **kernel_base;
  67. static short **kernel_end;
  68. static short *kernel_items;
  69.  
  70. /* hash table for states, to recognize equivalent ones.  */
  71.  
  72. #define    STATE_TABLE_SIZE    1009
  73. static core **state_table;
  74.  
  75.  
  76.  
  77. void 
  78. allocate_itemsets()
  79. {                /* WG */
  80.     register short *itemp;
  81.     register int symbol;
  82.     register int i;
  83.     register count;
  84.     register int max;
  85.     register short *symbol_count;
  86.  
  87.     count = 0;
  88.     symbol_count = NEW2(nsyms, short);
  89.  
  90.     itemp = ritem;
  91.     symbol = *itemp++;
  92.     while (symbol) {
  93.         if (symbol > 0) {
  94.             count++;
  95.             symbol_count[symbol]++;
  96.             }
  97.         symbol = *itemp++;
  98.         }
  99.  
  100.     /*
  101.      * see comments before new-itemset.  All the vectors of items live
  102.      * inside kernel_items.  The number of active items after some symbol
  103.      * cannot be more than the number of times that symbol appears as an
  104.      * item, which is symbol_count[symbol]. We allocate that much space
  105.      * for each symbol.  
  106.      */
  107.  
  108.     kernel_base = NEW2(nsyms, short *);
  109.     kernel_items = NEW2(count, short);
  110.  
  111.     count = 0;
  112.     max = 0;
  113.     for (i = 0; i < nsyms; i++) {
  114.         kernel_base[i] = kernel_items + count;
  115.         count += symbol_count[i];
  116.         if (max < symbol_count[i])
  117.             max = symbol_count[i];
  118.         }
  119.  
  120.     shift_symbol = symbol_count;
  121.     kernel_end = NEW2(nsyms, short *);
  122.     }
  123.  
  124.  
  125.  
  126. void 
  127. allocate_storage()
  128. {                /* WG */
  129.     allocate_itemsets();
  130.  
  131.     shiftset = NEW2(nsyms, short);
  132.     redset = NEW2(nrules + 1, short);
  133.     state_table = NEW2(STATE_TABLE_SIZE, core *);
  134.     }
  135.  
  136.  
  137.  
  138. void 
  139. free_storage()
  140. {                /* WG */
  141.     FREE(shift_symbol);
  142.     FREE(redset);
  143.     FREE(shiftset);
  144.     FREE(kernel_base);
  145.     FREE(kernel_end);
  146.     FREE(kernel_items);
  147.     FREE(state_table);
  148.     }
  149.  
  150.  
  151.  
  152. /* compute the nondeterministic finite state machine (see state.h for details)
  153. from the grammar.  */
  154.  
  155. void 
  156. generate_states()
  157. {                /* WG */
  158.     allocate_storage();
  159.     initialize_closure(nitems);
  160.     initialize_states();
  161.  
  162.     while (this_state) {
  163.         /*
  164.          * Set up ruleset and itemset for the transitions out of this
  165.          * state. ruleset gets a 1 bit for each rule that could
  166.          * reduce now. itemset gets a vector of all the items that
  167.          * could be accepted next.  
  168.          */
  169.         closure(this_state->items, this_state->nitems);
  170.         /* record the reductions allowed out of this state */
  171.         save_reductions();
  172.         /* find the itemsets of the states that shifts can reach */
  173.         new_itemsets();
  174.         /* find or create the core structures for those states */
  175.         append_states();
  176.  
  177.         /*
  178.          * create the shifts structures for the shifts to those
  179.          * states, now that the state numbers transitioning to are
  180.          * known 
  181.          */
  182.         if (nshifts > 0)
  183.             save_shifts();
  184.  
  185.         /* states are queued when they are created; process them all */
  186.         this_state = this_state->next;
  187.         }
  188.  
  189.     /* discard various storage */
  190.     finalize_closure();
  191.     free_storage();
  192.  
  193.     /* set up initial and final states as parser wants them */
  194.     augment_automaton();
  195.     }
  196.  
  197.  
  198.  
  199. /* Find which symbols can be shifted in the current state,
  200.    and for each one record which items would be active after that shift.
  201.    Uses the contents of itemset.
  202.    shift_symbol is set to a vector of the symbols that can be shifted.
  203.    For each symbol in the grammer, kernel_base[symbol] points to
  204.    a vector of item numbers activated if that symbol is shifted,
  205.    and kernel_end[symbol] points after the end of that vector.  */
  206.  
  207. void 
  208. new_itemsets()
  209. {                /* WG */
  210.     register int i;
  211.     register int shiftcount;
  212.     register short *isp;
  213.     register short *ksp;
  214.     register int symbol;
  215.  
  216. #ifdef    TRACE
  217.     fprintf(stderr, "Entering new_itemsets\n");
  218. #endif
  219.  
  220.     for (i = 0; i < nsyms; i++)
  221.         kernel_end[i] = NULL;
  222.  
  223.     shiftcount = 0;
  224.  
  225.     isp = itemset;
  226.  
  227.     while (isp < itemsetend) {
  228.         i = *isp++;
  229.         symbol = ritem[i];
  230.         if (symbol > 0) {
  231.             ksp = kernel_end[symbol];
  232.  
  233.             if (!ksp) {
  234.                 shift_symbol[shiftcount++] = symbol;
  235.                 ksp = kernel_base[symbol];
  236.                 }
  237.  
  238.             *ksp++ = i + 1;
  239.             kernel_end[symbol] = ksp;
  240.             }
  241.         }
  242.  
  243.     nshifts = shiftcount;
  244.     }
  245.  
  246.  
  247.  
  248. /* Use the information computed by new_itemset to find the state numbers
  249.    reached by each shift transition from the current state.
  250.   
  251.    shiftset is set up as a vector of state numbers of those states.  */
  252.  
  253. void 
  254. append_states()
  255. {                /* WG */
  256.     register int i;
  257.     register int j;
  258.     register int symbol;
  259.  
  260. #ifdef    TRACE
  261.     fprintf(stderr, "Entering append_states\n");
  262. #endif
  263.  
  264.     /* first sort shift_symbol into increasing order */
  265.  
  266.     for (i = 1; i < nshifts; i++) {
  267.         symbol = shift_symbol[i];
  268.         j = i;
  269.         while (j > 0 && shift_symbol[j - 1] > symbol) {
  270.             shift_symbol[j] = shift_symbol[j - 1];
  271.             j--;
  272.             }
  273.         shift_symbol[j] = symbol;
  274.         }
  275.  
  276.     for (i = 0; i < nshifts; i++) {
  277.         symbol = shift_symbol[i];
  278.         shiftset[i] = get_state(symbol);
  279.         }
  280.     }
  281.  
  282.  
  283.  
  284. /* find the state number for the state we would get to
  285. (from the current state) by shifting symbol.
  286. Create a new state if no equivalent one exists already.
  287. Used by append_states  */
  288.  
  289. int
  290. get_state(symbol)
  291.     int symbol;
  292. {
  293.     register int key;
  294.     register short *isp1;
  295.     register short *isp2;
  296.     register short *iend;
  297.     register core *sp;
  298.     register int found;
  299.  
  300.     int n;
  301.  
  302. #ifdef    TRACE
  303.     fprintf(stderr, "Entering get_state, symbol = %d\n", symbol);
  304. #endif
  305.  
  306.     isp1 = kernel_base[symbol];
  307.     iend = kernel_end[symbol];
  308.     n = iend - isp1;
  309.  
  310.     /* add up the target state's active item numbers to get a hash key */
  311.     key = 0;
  312.     while (isp1 < iend)
  313.         key += *isp1++;
  314.  
  315.     key = key % STATE_TABLE_SIZE;
  316.  
  317.     sp = state_table[key];
  318.  
  319.     if (sp) {
  320.         found = 0;
  321.         while (!found) {
  322.             if (sp->nitems == n) {
  323.                 found = 1;
  324.                 isp1 = kernel_base[symbol];
  325.                 isp2 = sp->items;
  326.  
  327.                 while (found && isp1 < iend) {
  328.                     if (*isp1++ != *isp2++)
  329.                         found = 0;
  330.                     }
  331.                 }
  332.  
  333.             if (!found) {
  334.                 if (sp->link) {
  335.                     sp = sp->link;
  336.                     }
  337.                 else {    /* bucket exhausted and no match */
  338.                     sp = sp->link = new_state(symbol);
  339.                     found = 1;
  340.                     }
  341.                 }
  342.             }
  343.         }
  344.     else {            /* bucket is empty */
  345.         state_table[key] = sp = new_state(symbol);
  346.         }
  347.  
  348.     return (sp->number);
  349.     }
  350.  
  351.  
  352.  
  353. /* subroutine of get_state.  create a new state for those items, if necessary.  */
  354.  
  355. core *
  356. new_state(symbol)
  357.     int symbol;
  358. {
  359.     register int n;
  360.     register core *p;
  361.     register short *isp1;
  362.     register short